﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using eCommerce.Core;
using eCommerce.Core.Domain.Products;
using eCommerce.Core.Domain.Securitys;
using eCommerce.Models.Product;
using eCommerce.Services.Brands;
using eCommerce.Services.Categorys;
using eCommerce.Services.Pictures;
using eCommerce.Services.Products;
using eCommerce.Services.Security;

namespace eCommerce.Controllers
{
    public class ProductsController : BaseController
    {
        private IWorkContext _workContext;
        private IPermissionService _permissionService;
        private IProductService _productService;
        private ICategoryService _categoryService;
        private IBrandService _brandService;
        private IPictureService _pictureService;

        public ProductsController(IWorkContext workContext, IPermissionService permissionService, IProductService productService, ICategoryService categoryService, IBrandService brandService, IPictureService pictureService)
        {
            _workContext = workContext;
            _permissionService = permissionService;
            _productService = productService;
            _categoryService = categoryService;
            _brandService = brandService;
            _pictureService = pictureService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Item(int itemId)
        {
            return null;
        }

        public ActionResult CreateProduct()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageProduct, _workContext.CurrentCustomer.Id))
            {
                return AccessDeniedView();
            }

            var category = _categoryService.GetDefaultCategory();
            var brand = _brandService.GetDefaultBrand();
            var productAttributeKeys = _productService.GetProductAttributeKeysByCatgory(category.Id);
            var itemAttributeKeys = _productService.GetItemAttributeKeysByCategory(category.Id);
            var model = new ProductModel();
            model.ShopId = _workContext.CurrentCustomer.ShopId.Value;
            model.ProductAttributeKeys = Mapper.Map<IList<ProductAttributeKeyModel>>(productAttributeKeys);
            model.ItemAttributeKeys = Mapper.Map<IList<ItemAttributeKeyModel>>(itemAttributeKeys);
            model.CategoryId = category.Id;
            model.BrandId = brand.Id;
            ViewBag.Model = model;
            return View("createproduct");
        }

        public ActionResult CreateProduct2()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageProduct, _workContext.CurrentCustomer.Id))
            {
                return AccessDeniedView();
            }

            var category = _categoryService.GetDefaultCategory();

            var brand = _brandService.GetDefaultBrand();

            var productAttributeKeys = _productService.GetProductAttributeKeysByCatgory(category.Id);

            var itemAttributeKeys = _productService.GetItemAttributeKeysByCategory(category.Id);

            var model = new ProductModel();
            model.ShopId = _workContext.CurrentCustomer.ShopId.Value;

            model.ProductAttributeKeys = Mapper.Map<IList<ProductAttributeKeyModel>>(productAttributeKeys);
            model.ItemAttributeKeys = Mapper.Map<IList<ItemAttributeKeyModel>>(itemAttributeKeys);
            model.CategoryId = category.Id;
            model.BrandId = brand.Id;
            ViewBag.Model = model;
            return View("createproduct2");
        }

        private IList<ProductPicModel> BuildProductPicModels()
        {
            var result = new List<ProductPicModel>();

            for (int i = 0; i < 3; i++)

            {
                result.Add(new ProductPicModel()
                {
                    ClientUploaderId = Guid.NewGuid().ToString().Replace("-",""),
                    DefaultProductPicUrl = _pictureService.GetDefaultPictureUrl(100),

                });
            }
            return result;
        }
        public ActionResult CreateItem(int productId)
        {
            var product = _productService.GetProduct(productId);

            var productAttributeValues = product.ProductAttributeValues;
            var itemAttributeValues = product.ItemAttributeValues;




            var itemCatalogGroups = new List<ItemCatalogGroup>();

            foreach (var productAttributeValue in productAttributeValues)
            {
                var itemCatalogGroup = new ItemCatalogGroup()
                {
                    ProductAttributeValueId = productAttributeValue.Id,
                    ProductAttributeValue = productAttributeValue,
                    UploaderIconPicClientId = Guid.NewGuid().ToString().Replace("-",""),
                    DefaultIconPicUrl = _pictureService.GetDefaultPictureUrl(100),
                    ProductPictures = BuildProductPicModels(),
                };
                var itemCatalogs= itemAttributeValues.Select(itemAttributeValue => new ItemCatalogModel()
                {
                    ItemAttributeValue = itemAttributeValue, 
                    ItemAttributeValueId = itemAttributeValue.Id,
                    ProductAttributeValue = productAttributeValue,
                    ProductAttributeValueId = productAttributeValue.Id
                }).ToList();


                itemCatalogGroup.ItemCatalogs = itemCatalogs;

                itemCatalogGroups.Add(itemCatalogGroup);
            }
        

            var itemModel = new ItemCatalogsModel()
            {
                ProductId = productId,

                ProductAttributeKey = product.ProductAttributeKey,
                ItemAttributeKey = product.ItemAttributeKey,
                UseProductAttribute = product.UseProductAttribute,
                UseItemAttribute = product.UseItemAttribute,
                ItemCatalogGroups = itemCatalogGroups,
            };


            

            return View("createitem", itemModel);
        }

        private ProductAttributeValueModel BuildProductAttributeValues(
            ProductAttributeKey productAttributeKey)
        {
            if (productAttributeKey == null)
                return null;
            var productAttributeValueModel = new ProductAttributeValueModel()
            {
                ProductAttributeKeyId = productAttributeKey.Id,
                ProductAttributeName = productAttributeKey.Name,
                Value = string.Empty
            };
            return productAttributeValueModel;
        }

        private ItemAttributeValueModel BuildItemAttributeValues(
            ItemAttributeKey itemAttributeKey)
        {
            if (itemAttributeKey== null)
                return null;

            var itemAttributeValueModel = new ItemAttributeValueModel()
            {
                ItemAttributeKeyId = itemAttributeKey.Id,
                ItemAttributeName = itemAttributeKey.Name,
                Value = string.Empty
            };
            return itemAttributeValueModel;
        }
    }
}